package com.wjh.controller;

import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.oauth.Oauth;
import com.wjh.bean.Constants;
import com.wjh.bean.Result;
import com.wjh.common.UrlCommon;
import com.wjh.feign.MemberServiceFeign;
import com.wjh.pojo.UserEntity;
import com.wjh.utils.CookiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/6/14.
 * @Modified By:
 */
@Controller
@RequestMapping("qq")
public class QQController extends UrlCommon{


    @Autowired
    MemberServiceFeign memberServiceFeign;
    /**
     * 跳转到QQ授权地址
     * @param request
     * @return
     * @throws QQConnectException
     */
    @GetMapping("/locaQQLogin")
    public String locaQQLogin(HttpServletRequest request) throws QQConnectException {
        //生成QQ授权连接
        String authorizeURL = new Oauth().getAuthorizeURL(request);
        return "redirect:"+authorizeURL;
    }


    @GetMapping("/callback")
    public String callback(HttpServletRequest request, HttpServletResponse response) throws QQConnectException {
        //获取AccessToken
        AccessToken accessToken = new Oauth().getAccessTokenByRequest(request);
        if (ObjectUtils.isEmpty(accessToken)) {
            request.setAttribute("error","QQ关联失败");
            return ERROR;
        }
        //获取openid
        OpenID openID = new OpenID(accessToken.getAccessToken());
        Result result = memberServiceFeign.findUserByOpenId(openID.getUserOpenID());
        //老用户关联账号
        if (result.getCode()== Constants.HTTP_RES_CODE_201) {
            request.setAttribute("qqOpenID",openID.getUserOpenID());
            return RELATION;
        }
        //老用户自动登陆
        if (result.getCode()== Constants.HTTP_RES_CODE_200) {
            LinkedHashMap linkedHashMap=(LinkedHashMap)result.getData();
            String token=(String) linkedHashMap.get("token");
            CookiesUtil.setCookie(response,Constants.MEMBER_TOKEN_COOKIE,token,Constants.MEMBER_TOKEN_COOKIE_TIME);
            return "redirect:/";
        }
        request.setAttribute("error",result.getMsg());
        return ERROR;
    }


    @PostMapping("qqbangding")
    public String qqbangding(UserEntity userEntity, HttpServletRequest request, HttpServletResponse response){
        Result result = memberServiceFeign.qqrelation(userEntity);
        if (result.getCode()!= Constants.HTTP_RES_CODE_200) {
            request.setAttribute("error",result.getMsg());
            return LOGIN;
        }
        LinkedHashMap  data =(LinkedHashMap) result.getData();
        String token = (String)data.get("token");
        if (ObjectUtils.isEmpty(token)) {
            request.setAttribute("error","会话已失效");
            return LOGIN;
        }
        CookiesUtil.setCookie(response,Constants.MEMBER_TOKEN_COOKIE,token,Constants.MEMBER_TOKEN_COOKIE_TIME);
        return "redirect:/";
    }
}
